﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApp.BaseDataSvc;
using YY.Common;

namespace WebApp
{
    public partial class EditCaseFrm : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(EditCaseFrm), this);
            var orderId = Request.Params["OrderId"];
            if (string.IsNullOrEmpty(orderId))
            {
                Response.Write("非法进入");
                Response.End();
            }
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<TB_Case> GetCase(int id)
        {
            AjaxResult<TB_Case> result = new AjaxResult<TB_Case>();
            result.Data = null;
            result.Msg = null;
            try
            {
                result.Data = this.BaseDataProxy.GetCase(id);
            }
            catch (Exception ex)
            {
                result.Data = null;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<int> SaveCase(string id, int orderId, string carNo, string check, string code, string driver, string memo, string model, string tel, string title, string trailer)
        {
            AjaxResult<int> result = new AjaxResult<int>();
            result.Data = 0;
            result.Msg = null;
            bool update = !string.IsNullOrEmpty(id);
            try
            {
                var order = this.BaseDataProxy.GetOrder(orderId);
                if (order.isSubmit)
                {
                    result.Msg = "已提交财务审核单据不能修改！";
                    return result;
                }

                TB_Case c = new TB_Case();
                WebCommon.EvalControlValueToData(c, "order", orderId);
                if (update)
                    c = this.BaseDataProxy.GetCase(Convert.ToInt32(id));
                WebCommon.EvalControlValueToData(c, "carNo", carNo);
                WebCommon.EvalControlValueToData(c, "check", check);
                WebCommon.EvalControlValueToData(c, "code", code);
                WebCommon.EvalControlValueToData(c, "driver", driver);
                WebCommon.EvalControlValueToData(c, "memo", memo);
                WebCommon.EvalControlValueToData(c, "model", model);
                WebCommon.EvalControlValueToData(c, "tel", tel);
                WebCommon.EvalControlValueToData(c, "title", title);
                WebCommon.EvalControlValueToData(c, "trailer", trailer);

                if (update)
                {
                    this.BaseDataProxy.UpdateCase(c);
                    result.Data = c.id;
                }
                else
                {
                    int newId = this.BaseDataProxy.AddCase(c);
                    result.Data = newId;
                }
            }
            catch (Exception ex)
            {
                result.Data = 0;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> DeleteCase(int id, int orderId)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                var order = this.BaseDataProxy.GetOrder(orderId);
                if (order.isSubmit)
                {
                    result.Msg = "已提交财务审核单据不能修改！";
                    return result;
                }

                result.Data = this.BaseDataProxy.DeleteCase(id);
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }
    }
}
